﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandUpgradeTunnelingClaws : BoCommand
    {
        private const float _min = 150.0f;
        private const float _gas = 150.0f;
        private const int _time = 110;
        private const commandID _commandID = commandID.upgradeTunnelingClaws;

        public BoCommandUpgradeTunnelingClaws()
        {
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.startBuildingUpgrade((int)Upgrades.TunnelingClaws);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingUpgrade((int)Upgrades.TunnelingClaws);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            bool isRequired = goal.isUpgradeRequired((int)Upgrades.TunnelingClaws);
            bool hasBeenSelected = node.gameState.isUpgradeBuilt((int)Upgrades.TunnelingClaws) || node.gameState.isUpgradeBeingBuilt((int)Upgrades.TunnelingClaws);
            if (isRequired && !hasBeenSelected)
            {
                return true;
            }
            node.dismissCommand((int)commandID.upgradeTunnelingClaws);
            return false;
        }
        public static bool isAvailable(BoGameState gs)
        {
            int nbBuildings = 0;
            int nbResearching = 0;
            nbBuildings = gs.nbBuildingsBuilt((int)Buildings.RoachWarren);
            nbResearching = (gs.isUpgradeBeingBuilt((int)Upgrades.GlialReconstitution)) ? (nbResearching + 1) : nbResearching;
            nbResearching = (gs.isUpgradeBeingBuilt((int)Upgrades.TunnelingClaws)) ? (nbResearching + 1) : nbResearching;

            if ((nbBuildings - nbResearching) < 1)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, 0))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100);
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Tunneling Claws";
            return str;
        }
    }
}
